'''

Case Type： 索引文件
Case Name： 索引文件丢失
Create At:  2020/07/01
Owner    :  @wan005
Descption:  
1、创建表，插入数据，加入索引
2、删除索引文件
3.利用索引进行查询
4、重启数据库
5、利用索引进行查询
6.索引重建
7.利用索引查询

'''
import os
import sys
import unittest
import _thread
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common


class IndexFileDamaged(unittest.TestCase):
    sh = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case069.py start------------')
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.com = Common()
        self.Constant = Constant()
        self.t_name = 't_test'
        self.t_index_name = 't_test_index'

    def test_del_indexfile(self):
        # 新建表
        
        self.log.info('----------------------------创建表，并写入数据-----------------------------')
        msg = self.com.createLargeNumTable(self.dbPrimaryDbUser, self.t_name, 1, 100000)
        self.log.info(msg)
        self.log.info('----------------------------创建索引---------------------------')
        sql_cmd = f"create unique index {self.t_index_name} on {self.t_name}(t_id);"
        self.log.info(sql_cmd)
        msg = self.sh.executDbSql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.Constant.CREATE_INDEX_SUCCESS_MSG, msg)

        # 写脏页避免影响启动
        sql_cmd = f"vacuum full"
        self.log.info(sql_cmd)
        msg = self.sh.executDbSql(sql_cmd)
        self.log.info(msg)

        # 查询索引oid
        self.log.info('----------------------------查询索引oid-----------------------------')
        sql_cmd = f"select oid from pg_class where relname='{self.t_index_name}';"
        self.log.info(sql_cmd)
        msg = self.sh.executDbSql(sql_cmd)
        self.log.info(msg)
        oid = msg.splitlines()[-2].strip()

        # 通过oid查询索引文件位置
        self.log.info('----------------------------通过oid查询索引文件位置-----------------------------')
        sql_cmd = f"select pg_relation_filepath({oid}::regclass);"
        self.log.info(sql_cmd)
        msg = self.sh.executDbSql(sql_cmd)
        self.log.info(msg)
        path = msg.splitlines()[-2].strip()
        self.log.info(path)

        self.log.info('-----------------------------删除索引文件---------------------------')
        rmpath = os.path.join(macro.DB_INSTANCE_PATH, path)
        self.log.info(rmpath)
        rm_cmd = 'rm -rf ' + rmpath
        self.log.info(rm_cmd)
        msg = self.dbPrimaryDbUser.sh(rm_cmd).result()
        self.assertIn('', msg)

        self.log.info('-------------------使用索引 fail---------------------')
        sql_cmd = f"select count(*) from {self.t_name} where t_id > 10;"
        self.log.info(sql_cmd)
        msg = self.sh.executDbSql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.Constant.NO_FILE_MSG, msg)

        time.sleep(180)

        self.log.info('----------------------重启数据库---------------------')
        is_stop = self.sh.stopDbCluster()
        self.assertTrue(is_stop)
        is_started = self.sh.startDbCluster()
        self.assertTrue(is_started)

        self.log.info('-------------------使用索引 fail---------------------')
        sql_cmd = f"select count(*) from {self.t_name} where t_id > 10;"
        self.log.info(sql_cmd)
        msg = self.sh.executDbSql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.Constant.NO_FILE_MSG, msg)

        self.log.info('-----------------------索引重建-----------------')
        sql = f' REINDEX INDEX {self.t_index_name};'
        result = self.sh.executDbSql(sql)
        self.log.info(result)
        self.assertIn('REINDEX',result)

        self.log.info('-------------------使用索引---------------------')
        sql_cmd = f"select count(*) from {self.t_name} where t_id > 10;"
        self.log.info(sql_cmd)
        msg = self.sh.executDbSql(sql_cmd)
        self.log.info(msg)
        self.assertIn('count', msg)

    def tearDown(self):
        # 清理环境，删除表
        self.log.info('-------------------this is tearDown--------------------')
        self.log.info('------------------delete table-----------------------')
        msg = self.com.createLargeNumTable(self.dbPrimaryDbUser, self.t_name, del_sign=True)
        self.log.info(msg)
